//
//  HCustomServiceView.m
//  逆向工程
//
//  Created by 9188 on 2017/5/22.
//  Copyright © 2017年 朱同海. All rights reserved.
//

#import "HCustomServiceView.h"

@interface HCustomServiceView ()

@property (nonatomic, strong) UILabel   *titleLabel;
@property (nonatomic, strong) UISwitch  *titleSwitch;


@end

@implementation HCustomServiceView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

#pragma mark - public
- (void)setTitleLabelText:(NSString *)title switchIsOn:(BOOL)isOn {
    self.titleLabel.text = title;
    self.on = isOn;
    [self.titleSwitch setOn:isOn];
}

- (void)switchIsOn:(BOOL)isOn {
    [self.titleSwitch setOn:isOn];
    self.on = isOn;
}

#pragma mark - private

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(15, 5, 200, 30);
    self.titleSwitch.frame = CGRectMake(self.frame.size.width-65, 5, 60, 35);
}

- (void)createUI {
    [self addSubview:self.titleLabel];
    [self addSubview:self.titleSwitch];
    [self addSubview:self.line];
}

- (void)switchAction:(UISwitch *)mySwitch {
    self.on = mySwitch.on;
    if (self.mySwitchBlock) {
        self.mySwitchBlock(mySwitch.on);
    }
}

#pragma mark - lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

- (UISwitch *)titleSwitch {
    if (!_titleSwitch) {
        _titleSwitch = [[UISwitch alloc] init];
        [_titleSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _titleSwitch;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(10, self.frame.size.height - 0.5, self.frame.size.width-20, 0.5)];
        _line.alpha = 0.5;
        _line.backgroundColor = [UIColor lightGrayColor];
    }
    return _line;
}

@end
